package io.renren.modules.wcs.listenSlaveDB;

import io.renren.common.utils.SpringContextUtils;
import io.renren.modules.basic.entity.MtCcTaskRunInfoEntity;
import io.renren.modules.wcs.listenSlaveDB.controller.MySqlBean;
import io.renren.modules.wcs.listenSlaveDB.controller.TestBean;
import io.renren.modules.wcs.listenSlaveDB.entity.TestEntity2;
import io.renren.modules.websocket.MessageModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 蔡翔
 * @Date: 2019/9/27 15:34
 * @Version 1.0
 */
public class SalveThread extends Thread {

    private String toName;

    public String getToName() {
        return toName;
    }

    public void setToName(String toName) {
        this.toName = toName;
    }

    private final static Logger logger = LoggerFactory.getLogger(SalveThread.class);


    TestBean sqlserverBean = (TestBean) SpringContextUtils.getBean("testBean");
    MySqlBean mySqlBean = (MySqlBean) SpringContextUtils.getBean("mySqlBean");
    MessageModule messageModule = (MessageModule) SpringContextUtils.getBean("messageModule");
    /*MtCcProductServiceBean mtCcProductServiceBean=(MtCcProductServiceBean) SpringContextUtils.getBean("mtCcProductServiceBean");
    MtCcTaskRunInfoServiceBean mtCcTaskRunInfoServiceBean=(MtCcTaskRunInfoServiceBean) SpringContextUtils.getBean("mtCcTaskRunInfoServiceBean");*/

    boolean flags = true;
    int count = 0;
    int newCount = 0;

    

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public synchronized void run(){
        System.err.println("====================== Come into SalveThread ======================");
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity=new MtCcTaskRunInfoEntity();
        while (true){
            while (flags){
                /*logger.info("i am running");*/
                newCount = sqlserverBean.count();
                if(count != newCount){
                    System.err.println("数据发生改变了。");
                    List<TestEntity2> testEntities = sqlserverBean.selectAll();

                    List<String> names = new ArrayList<>();
                    List<Integer> ids = new ArrayList<>();

                    if(!testEntities.isEmpty()){
                        for(TestEntity2 testEntity: testEntities){
                            ids.add(testEntity.getIdd());

                            /*Integer productId = mtCcProductServiceBean.getone(testEntity).getId();
                            mtCcTaskRunInfoEntity.setCreateTime(new Date());
                            mtCcTaskRunInfoEntity.setUserName("ERP");
                            mtCcTaskRunInfoEntity.setSource("ERP");
                            mtCcTaskRunInfoEntity.setTaskType(testEntity.getCrk());
                            if (("一号口").equals(testEntity.getCrko())){
                                if (testEntity.getCrk()==1){
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,"A1"));
                                }else {
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,"A1"));
                                }
                            }
                            if (("二号口").equals(testEntity.getCrko())){
                                if (testEntity.getCrk()==1){
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,"B1"));
                                }else {
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,"B1"));
                                }
                            }
                            if (("三号口").equals(testEntity.getCrko())){
                                if (testEntity.getCrk()==1){
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,"A2"));
                                }else {
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,"A2"));
                                }
                            }
                            if (("四号口").equals(testEntity.getCrko())){
                                if (testEntity.getCrk()==1){
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,"A3"));
                                }else {
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,"A3"));
                                }
                            }
                            if (("五号口").equals(testEntity.getCrko())){
                                if (testEntity.getCrk()==1){
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.IN_STOCK_ORDER_TYPE,"A4"));
                                }else {
                                    mtCcTaskRunInfoEntity.setTaskCode(ManagePublicMethod.creatOrderCode(ManageConstant.OUT_STOCK_ORDER_TYPE,"A4"));
                                }
                            }
                            mtCcTaskRunInfoEntity.setModel(testEntity.getFnumber());
                            mtCcTaskRunInfoEntity.setProductId(productId);
                            mtCcTaskRunInfoEntity.setNum(String.valueOf(testEntity.getFqty()));
                            mtCcTaskRunInfoEntity.setUnit(testEntity.getFunitid());
                            mtCcTaskRunInfoEntity.setFBillno(testEntity.getFbillno());
                            mtCcTaskRunInfoEntity.setFBatchNo(testEntity.getFbatchno());
                            mtCcTaskRunInfoEntity.setFEntryId(testEntity.getFentryid());
                            mtCcTaskRunInfoServiceBean.save(mtCcTaskRunInfoEntity);
                            mtCcTaskRunInfoEntity.setOrderNum(mtCcTaskRunInfoEntity.getId());
                            mtCcTaskRunInfoServiceBean.updateById(mtCcTaskRunInfoEntity);*/
                        }
                        Integer integer = sqlserverBean.deleteByBatchName(ids);
                        boolean b = mySqlBean.saveAll(testEntities);
                    }
                    /*List<TestEntity2> testEntities1 = mySqlBean.selectAll();
                    try {
                        boolean b = messageModule.sendMessageToAll("本次推送："+testEntities1.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/

                    newCount = 0;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.err.println(Thread.currentThread().getName()+"-线程意外退出了");
                    break;
                }
            }
            try {
                // 注意这里让线程 停顿个 0.1s很重要，当flags为false ，那就剩下 while(true){} 空跑了，这个线程虽然不会执行完但是会宕机down掉
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void shutdown(){
        this.flags = false;
    }
    public void wakeup(){
        this.flags = true;
    }
}
